//
//  SSSPaymentCodeViewController.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/25.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSPaymentCodeViewController.h"
#import "ZDQRCodeTool.h"

@interface SSSPaymentCodeViewController ()

{
    
    UILabel *headerLabel;
    UIImageView *_imageView;
    UIImageView *recodeImage;
    NSDictionary *dict;
}


/** 标签栏当前选中的按钮 */
@property(nonatomic, weak) UIButton *currentBtn;

@end

@implementation SSSPaymentCodeViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.title = @"收 款 码";
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpApperance];
    //[self reloadData];
    
}
- (void)setUpApperance{
    
    //背景
    UIImageView *bgImageView = [[UIImageView alloc]init];
    bgImageView.image = [UIImage imageNamed:@"img_bgpay"];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.frame = self.view.frame;
    [self.view addSubview:bgImageView];
    
    UIImageView *contentView = [[UIImageView alloc]initWithFrame:CGRectMake(HeigthIos6CompeteWithCurrent(50),64, KSCREEN_Width - WidthIos6CompeteWithCurrent(50) - WidthIos6CompeteWithCurrent(50), HeigthIos6CompeteWithCurrent(455))];
    contentView.image = [UIImage imageNamed:@"img_card"];
    [bgImageView addSubview:contentView];
    
        //1、借助UIImageView显示二维码
        recodeImage = [UIImageView new];
        [contentView addSubview:recodeImage];
       // recodeImage.backgroundColor = [UIColor redColor];
        recodeImage.sd_layout
        .centerXEqualToView(contentView)
        .topSpaceToView(contentView,HeigthIos6CompeteWithCurrent(135))
        .widthIs(WidthIos6CompeteWithCurrent(195))
        .heightIs(HeigthIos6CompeteWithCurrent(195));
    
    dict = @{@"userID":[SSSUserManager getUserModel].ID,@"1":@"付款"};
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSMutableString *registInfoStr = [NSMutableString stringWithString:jsonString];

    recodeImage.image = [ZDQRCodeTool ZD_generateWithDefaultQRCodeData:registInfoStr imageViewWidth:WidthIos6CompeteWithCurrent(195)];
    
    //保存按钮
    UIButton *saveBtn = [UIButton new];
    [saveBtn setTitle:@"保存图片" forState:UIControlStateNormal];
    saveBtn.titleLabel.font = [SSSFont systemFontOfSize:12];
    [saveBtn setTitleColor:[UIColor colorWithHex:@"5D9CB1"] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(savBtnClick) forControlEvents:UIControlEventTouchDown];
    [contentView addSubview:saveBtn];
    saveBtn.sd_layout
    .centerXEqualToView(contentView)
    .topSpaceToView(recodeImage,HeigthIos6CompeteWithCurrent(5))
    .widthIs(WidthIos6CompeteWithCurrent(50))
    .heightIs(HeigthIos6CompeteWithCurrent(12));
    
    
    UIImageView *scanPhoneImageView = [UIImageView new];
    scanPhoneImageView.image = [UIImage imageNamed:@"icon_scan"];
    [contentView addSubview:scanPhoneImageView];
    scanPhoneImageView.sd_layout
    .bottomSpaceToView(contentView,HeigthIos6CompeteWithCurrent(29))
    .leftSpaceToView(contentView,WidthIos6CompeteWithCurrent(73))
    .widthIs(WidthIos6CompeteWithCurrent(37))
    .heightIs(HeigthIos6CompeteWithCurrent(39));
    
    UILabel *descLabel = [UILabel new];
    descLabel.text = @"扫描二维码向商家付款";
    descLabel.textColor = [UIColor colorWithHex:@"666666"];
    descLabel.font = [SSSFont systemFontOfSize:15];
    descLabel.numberOfLines = 0;
    [contentView addSubview:descLabel];
    
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:descLabel.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3.0f];//设置行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, descLabel.text.length)];
    descLabel.attributedText = attributedString;
    //CGSize size = CGSizeMake(view.frame.size.width - 50, 41);
    //label.frame = CGRectMake(25, 25, [label sizeThatFits:size].width, [label sizeThatFits:size].height);
    descLabel.sd_layout
    .topEqualToView(scanPhoneImageView)
    .bottomEqualToView(scanPhoneImageView)
    .leftSpaceToView(scanPhoneImageView,WidthIos6CompeteWithCurrent(15))
    .widthIs(WidthIos6CompeteWithCurrent(80));
    descLabel.contentMode = UIViewContentModeTop;
    [descLabel sizeToFit];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img_line"]];
    [contentView addSubview:lineView];
    lineView.sd_layout
    .bottomSpaceToView(scanPhoneImageView,HeigthIos6CompeteWithCurrent(23))
    .centerXEqualToView(contentView)
    .widthIs(WidthIos6CompeteWithCurrent(238))
    .heightIs(1);
    
    
    
//    //上方的label
//    headerLabel = [UILabel new];
//    headerLabel.text = @"100%积分回馈";
//    headerLabel.textColor= [UIColor colorWithHex:@"55A987"];
//    headerLabel.font = [SSSFont systemFontOfSize:18];
//    headerLabel.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:headerLabel];
//    headerLabel.sd_layout
//    .topSpaceToView(self.view,HeigthIos6CompeteWithCurrent(33)+64)
//    .centerXEqualToView(self.view)
//    .widthIs(WidthIos6CompeteWithCurrent(300))
//    .heightIs(HeigthIos6CompeteWithCurrent(20));
//    
//    NSArray *titleArray = @[@"  100%积分",@"  75%积分",@"  50%积分"];
//    CGFloat width = (self.view.frame.size.width- WidthIos6CompeteWithCurrent(37)-WidthIos6CompeteWithCurrent(35))/3;
//    CGFloat height = HeigthIos6CompeteWithCurrent(20);
//    for (int index = 0; index<titleArray.count; index++) {
//        
//        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(index * width + WidthIos6CompeteWithCurrent(50),HeigthIos6CompeteWithCurrent(84)+64, width, height)];
//        btn.tag = index + 3000;
//        // 文字
//        [btn setTitle:titleArray[index] forState:UIControlStateNormal];
//        
//        btn.titleLabel.font = [SSSFont systemFontOfSize:13];
//        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//        // 颜色
//        [btn setTitleColor:[UIColor colorWithHex:@"6D6D6D"] forState:UIControlStateNormal];
//        // 按钮不能被点击时的颜色
//        [btn setTitleColor:[UIColor colorWithHex:@"55A987"] forState:UIControlStateDisabled];
//        [btn setImage:[UIImage imageNamed:@"icon_options"] forState:UIControlStateNormal];
//        [btn setImage:[UIImage imageNamed:@"icon_pitchon"] forState:UIControlStateDisabled];
//        
//        // 事件
//        [btn addTarget:self action:@selector(btnClik:) forControlEvents:UIControlEventTouchUpInside];
//        [btn sizeToFit];
//         [self.view addSubview:btn];
//        // 默认选中第一个按钮
//        if (index == 0) {
//            btn.enabled = NO;
//            self.currentBtn = btn;
//            // 让按钮那边的label根据文字内容计算尺寸
//            [btn.titleLabel sizeToFit]; // 方法1
//            //            self.indicatorView.width = [titles[idx] sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}].width;   // 方法2
//            
//        }
//    }
//    
//    //
//    //
//    _imageView = [[UIImageView alloc] init];
//    _imageView.image = [UIImage imageNamed:@"icon_car"];
//    [self.view addSubview:_imageView];
//    _imageView.sd_layout
//    .topSpaceToView(self.view,HeigthIos6CompeteWithCurrent(214)+64)
//    .leftSpaceToView(self.view,WidthIos6CompeteWithCurrent(92))
//    .rightSpaceToView(self.view,WidthIos6CompeteWithCurrent(64))
//    .bottomSpaceToView(self.view,HeigthIos6CompeteWithCurrent(228));
//    
//    //1、借助UIImageView显示二维码
//    recodeImage = [UIImageView new];
//    [self.view addSubview:recodeImage];
//    recodeImage.backgroundColor = [UIColor redColor];
//    recodeImage.sd_layout
//    .leftSpaceToView(self.view,WidthIos6CompeteWithCurrent(107))
//    .topSpaceToView(self.view,HeigthIos6CompeteWithCurrent(181)+64)
//    .widthIs(WidthIos6CompeteWithCurrent(134))
//    .heightIs(HeigthIos6CompeteWithCurrent(138));
//    
//    NSString *geneterQRCodeType;
//    geneterQRCodeType = GenerateQRCodePay;
//    switch (GenerateQRCodePay) {
//        case GenerateQRCodePay:
//            geneterQRCodeType = GenerateQRCodePay;
//            break;
//            
//        default:
//            break;
//    }
//    dict = @{@"userID":[SSSUserManager getUserModel].ID,@"1":@"付款"};
//    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
//    NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//    NSMutableString *registInfoStr = [NSMutableString stringWithString:jsonString];
//    
//    recodeImage.image = [ZDQRCodeTool ZD_generateWithDefaultQRCodeData:registInfoStr imageViewWidth:WidthIos6CompeteWithCurrent(134)];
//    
//    //收藏按钮
//    UIButton *saveBtn = [UIButton new];
//    [saveBtn setTitle:@"保 存 二 维 码" forState:UIControlStateNormal];
//    [saveBtn setTitleColor:[UIColor colorWithHex:@"55A987"] forState:UIControlStateNormal];
//    [saveBtn setBackgroundColor:[UIColor whiteColor]];
//    [saveBtn addTarget:self action:@selector(savBtnClick) forControlEvents:UIControlEventTouchDown];
//    [self.view addSubview:saveBtn];
//    saveBtn.sd_layout
//    .centerXEqualToView(self.view)
//    .bottomSpaceToView(self.view,HeigthIos6CompeteWithCurrent(139))
//    .widthIs(WidthIos6CompeteWithCurrent(271))
//    .heightIs(HeigthIos6CompeteWithCurrent(50));
//    saveBtn.layer.masksToBounds = YES;
//    saveBtn.layer.cornerRadius = 10;
//    saveBtn.layer.borderColor = [UIColor colorWithHex:@"40B87B"].CGColor;
//    saveBtn.layer.borderWidth = 0.5;
    
}
- (void)btnClik:(UIButton *)button{
    
    self.currentBtn.enabled = YES;
    button.enabled = NO;
    self.currentBtn = button;

    headerLabel.font = [SSSFont systemFontOfSize:18];
    headerLabel.text = [NSString stringWithFormat:@"%@回馈",self.currentBtn.titleLabel.text];
}
- (void)savBtnClick{
    
    
    
}
- (void)setQRCodeType:(GenerateQRCodeType)QRCodeType{
    
    _QRCodeType = QRCodeType;
    _QRCodeType = GenerateQRCodePay;
    NSString *geneterQRCodeType;
    dict = @{@"userID":[SSSUserManager getUserModel].ID,geneterQRCodeType:@"付款"};
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSMutableString *registInfoStr = [NSMutableString stringWithString:jsonString];
    
    recodeImage.image = [ZDQRCodeTool ZD_generateWithDefaultQRCodeData:registInfoStr imageViewWidth:WidthIos6CompeteWithCurrent(134)];
}
- (void)reloadData{
//    
    [ZDNetManager zd_requestWithType:ZDHttpRequestTypeGet UrlString:kConfigPath(kGenerateCode) Parameters:nil SuccessBlock:^(id response) {
        
        XHLog(@"---QRCode --%@",response);
        
        if ([response[@"status"] intValue] == 0) {
            
            NSString *QRCodeUrl;
            QRCodeUrl = response[@"data"][@"code"];
            XHLog(@"----%@",QRCodeUrl);
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                recodeImage.image = [ZDQRCodeTool ZD_generateWithDefaultQRCodeData:QRCodeUrl imageViewWidth:159];
            });
            
        }else{
            
            [QMUITips showError:response[@"message"] inView:self.view hideAfterDelay:2];
        }
        
    } FailureBlock:^(NSError *error) {
        
        XHLog(@"error--%@",error.localizedDescription);
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
        
    } progress:^(int64_t bytesProgress, int64_t totalBytesProgress) {
        
        
    }];

}
@end
